package com.itheima.mall.service.omsService;

import com.itheima.PageResult;
import com.itheima.mall.service.omsService.OmsOrderApi;
import com.itheima.mall.service.omsService.OmsOrderOperateHistoryApi;
import com.itheima.dto.OrderDto;
import com.itheima.pojo.oms.OmsOrder;
import com.itheima.pojo.oms.OmsOrderItem;
import com.itheima.pojo.oms.OmsOrderOperateHistory;
import com.itheima.vo.OmsOrderVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OmsOrderService {
    @DubboReference
    private OmsOrderApi omsOrderApi;
    @DubboReference
    private OmsOrderOperateHistoryApi omsOrderOperateHistoryApi;

    //分页
    public PageResult<OmsOrder> page(OrderDto orderDto, Integer pageNum, Integer pageSize) {
        return omsOrderApi.page(orderDto, pageNum, pageSize);
    }

    //批量删除
    public void deleteByIds(Integer[] ids) {
        omsOrderApi.deleteByIds(ids);
    }

    //批量关闭
    public void close(Integer[] ids, String note) {
        omsOrderApi.close(ids,note);
    }
    //批量发货
    public void delivery(List<Map<String, String>> list) {
        omsOrderApi.delivery(list);
    }
     //通过id获取订单信息
    public OmsOrderVo findById(Integer id) {
    OmsOrderVo omsOrderVo = new OmsOrderVo();
    //获取订单信息
        OmsOrder  omsOrder = omsOrderApi.findById(id);
        //获取商品信息
        List<OmsOrderItem> omsOrderItems = omsOrderApi.findItems(id);
        //获取操作记录
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryApi.findById(id);
        //将获取到的数据放入vo中
        BeanUtils.copyProperties(omsOrder,omsOrderVo);
        omsOrderVo.setOrderItemList(omsOrderItems);
        omsOrderVo.setHistoryList(omsOrderOperateHistories);
        return omsOrderVo;
    }

    public void updateMony(Map<String, Object> map) {

        Double discountAmount = (Double) map.get("discountAmount");
       Double freightAmount = (Double) map.get("freightAmount");
        Integer orderId =(Integer) map.get("orderId");
        Integer status = (Integer) map.get("status");
       omsOrderApi.updateMoney(discountAmount,freightAmount,orderId,status);
    }

    public void updateNote(Long id, String note, Integer status) {
        omsOrderApi.updateNote(id,note,status);
    }

    public void updateReceiver(Map<String, String> map) {
        omsOrderApi.updateReceiver(map);
    }
}
